/*
** my_printf.c for my_printf in /home/chassa_m/rendu/PSU_2013_my_printf
** 
** Made by chassa_m
** Login   <chassa_m@epitech.net>
** 
** Started on  Fri Nov 15 14:46:04 2013 chassa_m
** Last update Thu Nov 28 22:20:27 2013 chassa_m
*/

#include <stdarg.h>
#include "my.h"

int		my_fptr(int i, va_list ap)
{
  int		(*fptr[13])(va_list);

  fptr[0] = &flag_d;
  fptr[1] = &flag_d;
  fptr[2] = &flag_o;
  fptr[3] = &flag_x;
  fptr[4] = &flag_X;
  fptr[5] = &flag_u;
  fptr[6] = &flag_c;
  fptr[7] = &flag_s;
  fptr[8] = &flag_S;
  fptr[9] = &flag_p;
  fptr[10] = &flag_b;
  fptr[11] = &flag_m;
  fptr[12] = &flag_percent;
  if (i > 12)
    {
      my_putstr("// Woops ! Flag not recognized. :D //");
      return (0);
    }
  fptr[i](ap);
}

int		my_recognition(const char *format, char *flags, int i, va_list ap)
{
  while (*format != '\0')
    {
      if (*format == '%')
        {
	  format = format + 1;
	  while (*format == ' ')
	    format = format + 1;
	  while (flags[i] != *format && flags[i] != '\0')
	    i = i + 1;
	  my_fptr(i, ap);
	  i = 0;
	  format = format + 1;
	}
      if (*format != '%' && *format != '\0')
	{
	  my_putchar(*format);
	  format = format + 1;
	}
    }
}

int		my_printf(const char *format, ...)
{
  va_list	ap;
  char		*flags;
  int		i;

  i = 0;
  va_start(ap, format);
  flags = "dioxXucsSpbm%";
  my_recognition(format, flags, i, ap);
  va_end(ap);
}
